#include <string.h>
#include <stdio.h>

#include "lg_std.h"

#define LGOGL_CORE

#include "lgogl_std.h"


static const char* SHADER_BASE_CORE[] = {
"out vec4 outColor; \n"
"void main(){\n"
"    gl_Position = lgProjection*lgView*lgTransform*lgPosition;\n"
"    outColor = lgColor; \n"
"}\n",

"in vec4 outColor; \n"
"void main(){ \n"
"    gl_FragColor = outColor; \n"
"}\n"
};

static const char* SHADER_BASE_TEXTURE[] = {
"out vec2 outUv; \n"
"void main(){\n"
"    gl_Position = lgProjection*lgView*lgTransform*lgPosition;\n"
"    outUv = vec2(lgUv.x, lgUv.y);\n"
"}\n",

"in vec2 outUv; \n"
"uniform sampler2D lgBaseTexture;\n"
"void main(){ \n"
"    gl_FragColor = texture2D(lgBaseTexture, outUv);\n"
"}\n"
};

static const char** SHADER_SOURCES[] = {
		SHADER_BASE_CORE,
		SHADER_BASE_TEXTURE,
};

static struct {
	int ok;
}ctx = { 0 };

static LGShader*
newShader(LGStdShader e) {
    LGShader* shader = lgNewShader();
	const char** srcs = SHADER_SOURCES[e];
    lgShaderSource(shader,
		LGSHADER_VERTEX, srcs[0], strlen(srcs[0]));
    lgShaderSource(shader,
        LGSHADER_FRAGMENT, srcs[1], strlen(srcs[1]));
	char* err;
	if (lgCompileShader(shader, &err)) {
		printf("default shader error:%s\n", err);
	}
	return shader;
	//ctx.shaders[e] = shader;
}

static LGShader*
defaultShader(LGStdShader e) {
	return newShader(e);
}

static const char**
stdShaderSource(LGStdShader e) {
	return SHADER_SOURCES[e];
}

static LGStdBackend backend = {
	defaultShader,
	stdShaderSource,
};

LGOGL_API LGStdBackend*
lgoglStdBackend() {
	return &backend;
}

LGOGL_API void
lgoglInitStdContext(LGConfig* config, LGOGLConfig* oglConfig) {
}